package vin.response.study;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CheckServlet
 */
public class CheckServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CheckServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type", "text/html;charset=UTF-8");
		// TODO Auto-generated method stub
		if (request.getParameter("txtName") != null && request.getParameter("txtName").length() > 0) {
			String name = request.getParameter("txtName");// 获取form表单值

			response.getOutputStream().write((name + " ; " + "name.length() is:" + name.length()).getBytes());

		} else {

			String clientCheckcode = request.getParameter("validateCode");// 接收客户端浏览器提交上来的验证码
			String serverCheckcode = (String) request.getSession().getAttribute("checkcode");// 从服务器端的session中取出验证码
			if (clientCheckcode.toLowerCase().equals(serverCheckcode.toLowerCase())) {// 将客户端验证码和服务器端验证比较，如果相等，则表示验证通过
				response.getOutputStream().write("验证码验证通过！".getBytes());
				// System.out.println("验证码验证通过！");
			} else {
				response.getOutputStream().write("验证码验证失败！".getBytes());
				// System.out.println("验证码验证失败！");
			}
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
